package com.fanfanwang.ffw.app.tools;

import java.math.BigDecimal;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class MyTextUtils {
	private final static Pattern emailer = Pattern
			.compile("\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*");
	/**
	 * 判断是否是合法手机号
	 * 
	 * @param mobiles
	 * @return
	 */
	public static boolean isMobileNO(String mobiles) {
		Pattern p = Pattern
				.compile("^((13[0-9])|(15[^4,\\D])|(18[0,5-9])|(18[1,0-1]))\\d{8}$");
		Matcher m = p.matcher(mobiles);
		return m.matches();
	}

	public static boolean isMobilePaw(String mobilepaw) {
		Pattern p = Pattern.compile("^[A-Za-z0-9]+$");
		Matcher m = p.matcher(mobilepaw);
		return m.matches();
	}


	/**
	 * 判断是不是一个合法的电子邮件地址
	 * 
	 * @param email
	 * @return
	 */
	public static boolean isEmail(String email) {
		if (email == null || email.trim().length() == 0)
			return false;
		return emailer.matcher(email).matches();
	}
	/**
	 * 生成ID，机制：13位时间戳+1位随机数
	 * 
	 * @return
	 */
	public static String buildID() {
		String id = String.valueOf(System.currentTimeMillis())
				+ (int) (Math.random() * 10);
		return id;
	}
	
	public static String buildAlias() {
		String id = String.valueOf(System.currentTimeMillis())
				+ (int) (Math.random() * 1000);
		return id;
	}
	
	/**
	 * 生成ID，机制：13位时间戳+1位随机数
	 * 
	 * @return
	 */
	public static String buildFirstRandom_ID() {
		String id = (int) (Math.random() * 10)+String.valueOf(System.currentTimeMillis())
				+ (int) (Math.random() * 10);
		return id;
	}

	public static String buildThemeId(String time) {
		String id = "";
		try {
			id = TimeUtils.getDateYYYYMMDD(time);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return id;
	}
	
	public static String formatThemePath(String themeId) throws Exception{
		String formatPath = themeId.substring(0,4)+"/"+themeId.substring(4,8)+"/";
		return formatPath;
	}

	public static String save5Num(float num){
		BigDecimal bg = new BigDecimal(num);
        double j = bg.setScale(5, BigDecimal.ROUND_HALF_UP).doubleValue();
        return j+"";
	}
}
